<template>
  <div class="login_ccontainer">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录表单 -->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到甄选</h2>
          <!-- 账号 -->
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_button"
              type="primary"
              size="default"
              plain
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
// 引入router
// 获取账号密码
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElNotification } from 'element-plus'
// 引入获取当前时间的函数
import { getTime } from '@/utils/time'

// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user'

// 定义变量控制按钮加载效果
let loading = ref(false)

// 获取el-form组件
let loginForms = ref()

let $router = useRouter()
let userStore = useUserStore()

// 路由对象
let $route = useRoute()

let loginForm = reactive({ username: 'admin', password: 'atguigu123' })
// 登录按钮
const login = async () => {
  // 保证全部表单校验成功后才能发请求
  await loginForms.value.validate()

  // 开始加载效果
  loading.value = true
  // 通知仓库发登录请求
  // 请求成功跳转到首页
  // 请求失败弹出登录失败信息
  try {
    // 保证登录成功/跳转到home
    await userStore.userLogin(loginForm)
    // 编成式导航跳转到首页
    // 判断登录的时候,路由路径当中是否有query参数,如果有就往query参数跳转,
    // 没有就跳转到首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // 登录成功提示
    ElNotification({
      type: 'success',
      message: '登录成功!',
      title: `Hi,${getTime()}好`,
    })
    // 登录成功加载消失
    // 登录失败加载消失
    loading.value = false
  } catch (err: any) {
    // 登录失败提示
    // 登录失败加载消失
    loading.value = false
    ElNotification({
      type: 'error',
      message: err.message,
    })
  }
}

//自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  console.log(rule)

  //rule:即为校验规则对象
  //value:即为表单元素文本内容
  //函数:如果符合条件callBack放行通过即为
  //如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  console.log(rule)

  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}

// 定义表单校验需要的配置对象
const rules = {
  username: [
    // rigger:触发校验表单的时机,change->文本发生变化触发校验,blur:失去焦点时发生校验
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [{ trigger: 'change', validator: validatorPassword }],
}
</script>

<style scoped lang="scss">
.login_ccontainer {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }
    .login_button {
      width: 100%;
    }
  }
}
</style>
